package com.example.qwen02.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/ai")
@CrossOrigin(origins = "*")
public class QwenController {
//    List<String> messages = new ArrayList<>();
    @RequestMapping("/qwen")
    @ResponseBody
    public Map<String, Object> getQwen(String question, int maxTokens, HttpSession session) throws NoApiKeyException, InputRequiredException {
//        System.out.println(session.getId());
        List<String> messages = (List<String>) session.getAttribute("history");
        if (messages == null) {
            messages = new LinkedList<>();
            session.setAttribute("history", messages);
        }

        messages.add("用户说：" + question);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < messages.size(); i++) {
            sb.append(messages.get(i));
            sb.append("\n");
        }

        String prompt = "你是一个小助手，回答用户的问题";
        Generation gen = new Generation();
        QwenParam params = QwenParam.builder().model("qwen-turbo")
//                .prompt(prompt + "用户说:" + question)
                .prompt(prompt + sb.toString())
                .seed(222)
                .topP(0.8)
                .resultFormat("message")
                .enableSearch(false)
                .maxTokens(maxTokens)
                .temperature((float) 0.85)
                .repetitionPenalty((float) 1.0)
                .build();
        GenerationResult result = gen.call(params);
        System.out.println(result);

        String answer = result.getOutput().getChoices().get(0).getMessage().getContent();
        messages.add("小助手说：" + answer);
        // 记忆体不应该超过3轮
        if (messages.size() >= 6) {
            messages.remove(0);
            messages.remove(0);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("question", question);
        map.put("answer", answer);
        map.put("time", new Date());
        return map;
    }
}
//    @RequestMapping("/qwen")
//    @ResponseBody
//    public GenerationResult getQwen(String prompt,int maxTokens) throws NoApiKeyException, InputRequiredException {
//        Generation gen = new Generation();
//        QwenParam params = QwenParam.builder().model("qwen-turbo")
////                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
//                .prompt(prompt)
//                .seed(222)
//                .topP(0.8)
//                .resultFormat("message")
//                .enableSearch(false)
//                .maxTokens(maxTokens)
//                .temperature((float)0.85)
//                .repetitionPenalty((float)1.0)
//                .build();
//        GenerationResult result = gen.call(params);
//        return result;
//    }